﻿using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mIE.UI
{
    public partial class Frm_IE_TaskList_Update : frmBaseChild
    {
        DataTable dataTable=null;
        public Frm_IE_TaskList_Update()
        {
            InitializeComponent();
        }

        private void Frm_IE_TaskList_Update_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }

        private void Frm_IE_TaskList_Update_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = this.Width - 330;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strDWG_NO = vGridControl1.Rows["rowDWG_NO"].Properties.Value != null ?
                      vGridControl1.Rows["rowDWG_NO"].Properties.Value.ToString() : "";//图纸编号
                string strPART_NO = vGridControl1.Rows["rowPART_NO"].Properties.Value != null ?
                    vGridControl1.Rows["rowPART_NO"].Properties.Value.ToString() : "";//部件编号
                string strFILE_NO = vGridControl1.Rows["rowFILE_NO"].Properties.Value != null ?
                    vGridControl1.Rows["rowFILE_NO"].Properties.Value.ToString() : "";//文件编号
                string strREF_MAT = vGridControl1.Rows["rowREF_MAT"].Properties.Value != null ?
                    vGridControl1.Rows["rowREF_MAT"].Properties.Value.ToString() : "";//参考物料


                if (string.IsNullOrEmpty(strDWG_NO))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("图纸编号为空,不允许保存！", "提示", MessageBoxButtons.OK);
                    return;
                }
                if (string.IsNullOrEmpty(strPART_NO))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("部件编号为空,不允许保存！", "提示", MessageBoxButtons.OK);
                    return;
                }
                if (DevExpress.XtraEditors.XtraMessageBox.Show("确定后会删除如下数据表：" +
                    "\r\n【尺寸参数表】" +
                    "\r\n【工艺路线表】" +
                    "\r\n【部件特征表】" +
                    "\r\n【周期表】" +
                    "\r\n【工时结果表】", "是否确定修改", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    if (Common.GetCommon.Update_Delete_Table(strDWG_NO, strPART_NO, strFILE_NO, strREF_MAT))
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("记录修改成功！", "提示", MessageBoxButtons.OK);
                        //每次保存后锁定信息输入
                        for (int i = 0; i < vGridControl1.Rows.Count; i++)
                        {
                            vGridControl1.SetCellValue(vGridControl1.Rows[i], 0, null);
                        }
                        DoView(null);
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("记录修改失败！", "提示", MessageBoxButtons.OK);
                        return;
                    }
                }

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        public override void DoSave(Lamex.Interface.IButtonInfo sender)
        {
            btnSave_Click(null, null);
        }
        public override void DoView(IButtonInfo sender)
        {
            string strDWG_NO = txtDWG_NO.Text;
            string strPART_NO = txtPART_NO.Text;
            if (string.IsNullOrEmpty(strDWG_NO))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("图纸编号不允许为空！", "提示", MessageBoxButtons.OK);
                return;
            }
            if (string.IsNullOrEmpty(strPART_NO))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("部件编号不允许为空！", "提示", MessageBoxButtons.OK);
                return;
            }
            string sql = @"SELECT [PROD_CATEGORY] as '产品类型'
      ,[FILE_NO] as '文件编号'
      ,[DWG_NO] as '图纸编号'
      ,[PART_NO] as '部件编号'
      ,[REF_DWG] as '参考图纸'
      ,[REF_MAT] as '参考号码'
      ,[IMPORT_TIME] as '导入时间'
      ,[IMPORT_USER] as '导入用户'
      ,[ROUTING_FLAG] as '工艺标识'
      ,[PARM_FLAG] as '尺寸标识'
      ,[FEAT_FLAG] as '特征标识'
      ,[REF_FLAG] as '参考标识'
      ,[CAL_FLAG] as '计算标识'
      ,[UPLOAD_FLAG] as '上载标识'
  FROM [LamexMESManager].[dbo].[IE_Part_TaskList]
  WHERE DWG_NO='" + strDWG_NO + "' AND PART_NO='" + strPART_NO + "' AND ISNULL(LOCK_FLAG,'')=''";
            dataTable = new DataTable();
            dataTable = BaseBuildSql.GetDataTableBySql(sql);
            gridControl1.DataSource = dataTable;
            gridView1.BestFitColumns();
            this.gridView1.OptionsView.ColumnAutoWidth = false;
        }
        public override void DoEdit(Lamex.Interface.IButtonInfo sender)
        {
            SetVGridControl();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            SetVGridControl();
        }
        private void SetVGridControl()
        {
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                //获取当前行的数据
                int iRow = gridView1.FocusedRowHandle;
                string strFILE_NO = gridView1.GetRowCellDisplayText(iRow, "文件编号").ToString();//文件编号
                string strDWG_NO = gridView1.GetRowCellDisplayText(iRow, "图纸编号").ToString();//图纸编号
                string strPART_NO = gridView1.GetRowCellDisplayText(iRow, "部件编号").ToString();//部件编号
                string strREF_MAT = gridView1.GetRowCellDisplayText(iRow, "参考号码").ToString();//参考图纸

                //把数据显示到右边的VGridControl
                vGridControl1.Rows["rowDWG_NO"].Properties.Value = strDWG_NO;
                vGridControl1.Rows["rowPART_NO"].Properties.Value = strPART_NO;
                vGridControl1.Rows["rowFILE_NO"].Properties.Value = strFILE_NO;
                vGridControl1.Rows["rowREF_MAT"].Properties.Value = strREF_MAT;
                //定位到文件编号
                vGridControl1.FocusedRow = vGridControl1.Rows["rowFILE_NO"];
            }
            else
            {
                for (int i = 0; i < vGridControl1.Rows.Count; i++)
                {
                    vGridControl1.SetCellValue(vGridControl1.Rows[i], 0, null);
                }
            }
        }
    }
}
